import yaml
from frame_work.setting import base_Dir
import os

# 封装读取写入yaml文件


class YAML():
    def __init__(self, path):
        self.path = path  # 文件路径
        # self.param = param  # 读取信息的key,不传默认获取所有数据

    # 获取yaml文件中的数据
    def get_data(self, param=None):
        """
        :param: # 读取信息的key,不传默认获取所有数据
        :return:
        """
        with open(self.path, 'rb') as y:
            content = y.read()  # 获取yaml文件中的所有信息
        # yaml.warnings({'YAMLLoadWarning': False})  # 禁用加载器warnings报警
        cf = yaml.unsafe_load(content)  # 将bytes格式转成dict格式
        # y.close()  # 关闭文件
        if param is None:
            return cf  # 返回所有数据
        else:
            return cf.get(param)  # 获取键为param的值

    # 追加yaml文件数据
    def echo_data(self, message):
        """
        写入数据
        :param message: 值
        :return:
        """
        with open(self.path, "a+", encoding="utf-8") as f:
            # 注：此模式为追加模式,若想直接重写则将open函数中的模式'a+'改为'w'
            yaml.dump(message, f)


if __name__ == "__main__":
    # p = "Z:\\readwrite.yaml"
    yamld = YAML(os.path.join(base_Dir, 'data', 'data.yaml'))
    print(yamld.get_data('domain'))
    dict1 = {
        'url': 'http://user.sk3.com/seller/tryings/save_try_base',
        'data': {
            'tid': '',
            'is_qr_order': 0,
            'order_type': 1,
            'is_quick_order': 0,
            'IsPostPay': 0,
            'DateNum': 22,
            'img_wx': '',
            'Img_check_wx': '',
            'cate_cid': 15,
            'Img_check': '',
            'GrantNum': 22,
            'exchangeNum': 0,
            'Price': 50,
            'try_url_order[]': '',
            'shopname': '',
            'before_order_price': 99,
            'after_order_price': 22,
            'order_quantity': 1,
            'coupons_url': '',
            'specs_limit': 1,
            'pattern': 'dxx-1',
            'item_No': 'term-01',
            'weight': '12g',
            'spec': '大码',
            'style': '中国红',
            'volume': 12,
            'size': 'M',
            'color': '极光粉',
            'other_specs': 'other',
            'default_shopping': '京东快递',
            'delivery_fee_limit': 3,
            'url_favorites': ''},
        'header': {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}}
    # yamld.echo_data(dict1)
    import string
